﻿package org.openChart.meta.data 
{
	import flash.media.Sound;
	import org.openChart.meta.BaseObject;
	import org.openChart.meta.Style;
	
	/**
	 * ...
	 * @author Chongyuan
	 */
	public class DataGroup extends BaseObject
	{
		private var elements:Array;
		private var length_:uint = 0;
		private var minValue_:Number = Number.MAX_VALUE;
		private var maxValue_:Number = Number.MIN_VALUE;
		private var total_:Number = 0;
		private var title_:String;
		private var label_:String;
		
		public function DataGroup( object:Style, elements:Array = null ) 
		{
			if (elements == null)
				this.elements = new Array();
			super( object );
		}
		
		public function addElement(element:DataElement):void
		{
			this.elements.push(element);
			total_ += element.getValue();
			this.length_++;
			if (minValue_ > element.getValue() )
			{
				minValue_ = element.getValue();
			}
			if (maxValue_ < element.getValue() )
			{
				maxValue_ = element.getValue();
			}
		}
		
		public function getAt( index:uint ):DataElement
		{
			if (index < 0 || index >= length)
				throw new Error("index out of boundary!");
			return elements[index] as DataElement;
		}
		
		public function clear():void
		{
			for (var i:uint = 0; i < length_; i++)
			{
				elements.pop();
			}
			length_ = 0;
			maxValue_ = Number.MIN_VALUE;
			minValue_ = Number.MAX_VALUE;
		}
		
		public function get maxValue():Number
		{
			return this.maxValue_;
		}
		
		public function get minValue():Number
		{
			return this.minValue_;
		}
		
		public function get length():uint
		{
			return length_;
		}
		
		public function get total():Number
		{
			return total_;
		}
		
		public function set label( label_:String ):void
		{
			this.label_ = label_;
		}
		
		public function get label():String
		{
			return label_;
		}
		
		public function toString():String
		{
			var str:String = "";
			str += "Object:DataGroup ";
			str += "[length = " + length + "]";
			str += "[label = " + label + "]";
			//str += "[title = " + title + "]";
			str += "[maxValue = " + maxValue + "]";
			str += "[minValue = " + minValue + "]";
			str += "[total=" + total + "] \n";
			for (var i:uint = 0 ; i < length; i++ )
			{
				str += getAt(i);
			}
			str += "\n"
			return str;
		}
	}
	
}